using System.Web.Services;
using System.Web;
using System.Xml;
using System;
using umlungu.engineX.blogX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2008-06-14
	Status:		release	
	Version:	2.0.0
	Build:		20080614
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20080614:	Initial release.
	---------------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX.WebsiteX {
	/// <summary>
	/// Description of the classX class.
	/// <para>Additional information about the class</para>
	/// </summary>
	public class WebsiteXDisplayX : profileX {
		#region Invisible properties
		private x_broker broker;
		#endregion

		#region Visible properties
		private string property;
		/// <summary>Definiton of the property</summary>
		/// <value>Description of the value of the property</value>
		public string Property {
			get { return property; }
			set { property = value; }
		}
        private WebsiteXCookies cooker;
        /// <summary>The WebmailX cookie manager object</summary>
        public WebsiteXCookies Cooker{
            get { return cooker; }
        }

        private WebsiteXUser user;
		/// <summary>The webmail user object</summary>
		public WebsiteXUser WebsiteUser {
			get { return user; }
		}
		#endregion

		#region Constructors/Destructors
		/// <summary>Default constructor</summary>
		public WebsiteXDisplayX() : base("WebsiteX") {
            cooker = new WebsiteXCookies(this);
		}
		/// <summary>Constructor for derived classes</summary>
		public WebsiteXDisplayX(string label) : base(label) {
            cooker = new WebsiteXCookies(this);
		}
		#endregion

		#region Public methods
		#endregion
		
		#region Protected methods
        protected override void _Initialise() {
			base._Initialise();
			user = new WebsiteXUser(UserProfile);
		}

		/// <summary>Initialises the page and adds custom arguments to be passed to the xsl processor</summary>
		protected override void _InitialiseTemplate() {
			base._InitialiseTemplate();
			Debugger.Spoor("_InitialiseTemplate", "skin", Cooker.Skin, "email", user.Username, "fullname", user.Fullname);
			TemplateArguments.AddParam("skin", "", Cooker.Skin);
			TemplateArguments.AddParam("user", "", user.Username);
			TemplateArguments.AddParam("person", "", user.Fullname);
		}

		/// <summary>
		/// Does the pre-processing of the page request. 
		///	This method is overriden in order to:
		///	- Add form fields to the profile
		/// </summary>
		protected override void _ProcessRequest() {
			UserProfile.Add(Request);
			
            switch (Parameters.Broker) {
				// Example broker
				case "example":			    broker = new WebsiteXExample(this);	broker.Process("test");	                break;
                
                //survey broker
                case "survey":              broker = new WebsiteXSurvey(this, "200805"); broker.Process();              break;

                //search broker
                case "search":              broker = new WebsiteXSearch(this);    broker.Process("search");               break;

                // WebmailPassportX related brokers
                case "passporttest":        broker = new WebsiteXPassport(this);    broker.Process("test");             break;
                case "passportlogin":       broker = new WebsiteXPassport(this);    broker.Process("login");            break;
                case "passportregister":    broker = new WebsiteXPassport(this);    broker.Process("register");         break;
                case "passportchange":      broker = new WebsiteXPassport(this);    broker.Process("change");           break;
                case "passportforgot":      broker = new WebsiteXPassport(this);    broker.Process("forgot");           break;
                case "passportsummary":     broker = new WebsiteXPassport(this);    broker.Process("summary");          break;
                case "passportunlock":      broker = new WebsiteXPassport(this);    broker.Process("unlock");           break;
                case "passportlogout":      broker = new WebsiteXPassport(this);    broker.Process("logout");           break;
                case "passporttoken":       broker = new WebsiteXPassport(this);    broker.Process("token");            break;
                case "passporthint":        broker = new WebsiteXPassport(this);    broker.Process("hint");             break;
                case "passportcheckhint":   broker = new WebsiteXPassport(this);    broker.Process("checkhint");        break;
                case "passportshare":       broker = new WebsiteXPassport(this);    broker.Process("share");            break;

                // Blog related brokers
                case "blogview":            broker = new WebsiteXBlogs(this);   broker.Process("view");                 break;
                case "blogcomment":         broker = new WebsiteXBlogs(this);   broker.Process("comment");              break;
                case "blogapprove":         broker = new WebsiteXBlogs(this);   broker.Process("approve");              break;
                case "blogdelete":          broker = new WebsiteXBlogs(this);   broker.Process("delete");               break;

                //Shop Cart
                case "addItem":             broker = new WebsiteXShop(this);    broker.Process("addItem");              break;
                case "remItem":             broker = new WebsiteXShop(this);    broker.Process("remItem");              break;
                case "editItemPlus":        broker = new WebsiteXShop(this);    broker.Process("editItemPlus");         break;
                case "editItemMinus":       broker = new WebsiteXShop(this);    broker.Process("editItemMinus");        break;
                case "checkShopCartEmpty":  broker = new WebsiteXShop(this);    broker.Process("checkShopCartEmpty");   break;

                //Newsletter functions
                case "validateEmail":       broker = new WebsiteXNewsletter(this); broker.Process("validateEmail");     break;
                case "subscribe":           broker = new WebsiteXNewsletter(this); broker.Process("subscribe");         break;
                case "unSubscribe":         broker = new WebsiteXNewsletter(this); broker.Process("unSubscribe");       break;

                // Tests
				case "errortest":		throw new x_exception("error_test", "This is an error test");
				case null:
				case "":				break;
				// use base broker for default (ie sends email on submission)
				default:				broker = new x_broker(this);			broker.Process();	break;
			}
		}
		/// <summary>
		/// Does the post-processing of the page content. 
		///	This method is overriden in order to:
		///	- add feedback and login xml
		///	- post process the xml content through a broker (ie extend xml added via page.xml)
		/// </summary>
        protected override void _ProcessContent() {
            Debugger.Spoor("_ProcessContent");
            if (Parameters.YesNo("relinfo", true)) {
                XmlElement relEl = (XmlElement)Content.AppendChild(Document.CreateElement("relinfo"));
                if (Parameters.YesNo("feed"))	// ie off by default
                    loadFeedBack(relEl);
                if (Parameters.YesNo("login"))	// ie off by default
                    loadLogon(relEl);
                //add shopcart
                if (Parameters.YesNo("shopcart", true)){
                    XmlElement cartEl = (XmlElement)Content.AppendChild(Document.ImportNode(UserProfile.Node("shopcart"), true));
                }
            }
            switch (Parameters.Broker) {
                case "example":		((WebsiteXExample)broker).ProcessContent(Content);	break;
            }
        }
        #endregion
				
		#region Private methods
		/// <summary>Load the feedback element</summary>
		private void loadFeedBack(XmlElement thisel) {
			WebsiteXFeed thisfeed = new WebsiteXFeed(Application);
			thisfeed.Add(Page, thisel);
			XmlNode lnkname = thisel.SelectSingleNode("feedback/page/fields/fld[@name='LinkName']/value");
			if (lnkname != null)
				lnkname.InnerText = Parameters.LinkName;
		}
		/// <summary>Load the login element</summary>
		private void loadLogon(XmlElement thisel) {
			WebsiteXLogon thislogon = new WebsiteXLogon(Application);
			thislogon.Add(Page, thisel);
		}
		#endregion
	}
}
